#include "chat.h"
#include "scene_logic.h"
#include "MapSlot.h"
#include "script_define.h"
#include "union.h"

static const uint32_t type_creat_guild = 1;				//创建公会
static const uint32_t type_dissolve_guild = 2;			//解散公会

static const uint32_t type_player_join_guild = 1;		//加入公会
static const uint32_t type_player_secede_guild = 2;		//退出公会

static const uint32_t type_chat_to_world = 1;			//世界聊天
static const uint32_t type_chat_to_guild = 2;			//公会聊天
static const uint32_t type_chat_to_player = 3;			//私人聊天

void CChatManager::SendChatMessageToClient(uint32_t myPid, uint32_t type
	, uint32_t destId, const char* pContant, Player& pMyself, uint16_t horn
	, uint16_t shareType)
{
	_safe_guard_begin;

	//替换非法字串
	// 	std::string retNewStr = "";
	// 	if (nullptr != m_chat_player_mgr_lua_ptr)
	// 	{
	// 		retNewStr = m_chat_player_mgr_lua_ptr->RunScriptFunctionStringRet(LUA_ChatFunctionScript
	// 			, LUA_ReplacementPlayerChatCharacter, pContant);
	// 	}

	// 	if (0 == retNewStr.length())
	// 	{
	// 		retNewStr = "** ** **.";
	// 	}
	// 
	// 	pContant = retNewStr.c_str();

	switch (type)
	{
	case type_chat_to_world:
	{
		for (auto itor : CSceneLogic::GetInstance().GetPlayerIdMap())
		{
			Player * _tmp_p = CSceneLogic::GetInstance().GetPlayerByPlayerID(itor.first);
			if (nullptr == _tmp_p)
			{
				continue;
			}
			_tmp_p->PushChatMessageToClient(type, pContant
				, myPid
				, pMyself.GetAccount()
				, pMyself.GetHeroId()
				, pMyself.GetVip()
				, pMyself.GetName()
				, horn
				, shareType);

			if (2 == horn)//使用大喇叭^v^ (0:无喇叭, 1:小喇叭, 2:大喇叭)
			{
				//推送跑马灯
				_tmp_p->SendGmMarqueeToClient(1, 5, pContant, pMyself.GetName(), 1);
			}
		}

		//世界聊天只存一次就够了
		pMyself.SaveChatRecord(type_chat_to_world, destId, pContant, pMyself, horn, shareType);

		pMyself.SendChatRetMessageToClient(0);

		break;
	}
	case type_chat_to_guild:
	{
		SendChatMessageToGuild(myPid, type, destId, pContant, pMyself, shareType);

		pMyself.SaveChatRecord(type, destId, pContant, pMyself, horn, shareType);

		pMyself.SendChatRetMessageToClient(0);

		break;
	}
	case type_chat_to_player:
	{
		Player * _tmp_p = CSceneLogic::GetInstance().GetPlayerByPlayerID(destId);
		if (nullptr != _tmp_p)
		{
			_tmp_p->PushChatMessageToClient(type, pContant
				, myPid
				, pMyself.GetAccount()
				, pMyself.GetHeroId()
				, pMyself.GetVip()
				, pMyself.GetName());

			_tmp_p->SaveChatRecord(type_chat_to_player, destId, pContant, pMyself);


			pMyself.PushChatMessageToClient(type, pContant
				, myPid
				, pMyself.GetAccount()
				, pMyself.GetHeroId()
				, pMyself.GetVip()
				, pMyself.GetName());

			destId = pMyself.GetPlayerId();
			pMyself.SaveChatRecord(type_chat_to_player, destId, pContant, pMyself);
			pMyself.SendChatRetMessageToClient(0);
		}
		else
		{
			//回复给自己：兄弟，对方没在线
			//ret:1
			pMyself.SendChatRetMessageToClient(1);
		}
		break;
	}
	default:
		break;
	}

	_safe_guard_end;
}

void CChatManager::SendChatMessageToClient(uint32_t myPid, uint32_t type
	, uint32_t destId, const char* pContant, Player& pMyself
	, const char* gvcId, double gvcTime, const char* gvcPart)
{
	_safe_guard_begin;

	//替换非法字串
	std::string retNewStr = "";
	retNewStr = MapSlot::GetInstance().GetLuaManagerPtr()->RunScriptFunctionStringRet(LUA_ChatFunctionScript
		, LUA_ReplacementPlayerChatCharacter, pContant);

	if (0 == retNewStr.length())
	{
		retNewStr = "** ** **.";
	}

	pContant = retNewStr.c_str();

	switch (type)
	{
	case type_chat_to_world:
	{
		for (auto itor : CSceneLogic::GetInstance().GetPlayerIdMap())
		{
			Player * _tmp_p = CSceneLogic::GetInstance().GetPlayerByPlayerID(itor.first);
			if (nullptr == _tmp_p)
			{
				continue;
			}
			_tmp_p->PushChatMessageToClient(type, pContant
				, myPid
				, pMyself.GetAccount()
				, pMyself.GetHeroId()
				, pMyself.GetVip()
				, pMyself.GetName()
				, gvcId
				, gvcTime
				, gvcPart);
		}

		//世界聊天只存一次就够了
		pMyself.SaveChatRecord(type_chat_to_world, destId, pContant, pMyself, gvcId, gvcTime, gvcPart);

		pMyself.SendChatRetMessageToClient(0);

		break;
	}
	case type_chat_to_guild:
	{
		SendChatMessageToGuild(myPid, type, destId, pContant, pMyself
			, gvcId
			, gvcTime
			, gvcPart);

		pMyself.SaveChatRecord(type, destId, pContant, pMyself, gvcId, gvcTime, gvcPart);

		pMyself.SendChatRetMessageToClient(0);

		break;
	}
	case type_chat_to_player:
	{
		Player * _tmp_p = CSceneLogic::GetInstance().GetPlayerByPlayerID(destId);
		if (nullptr != _tmp_p)
		{
			_tmp_p->PushChatMessageToClient(type, pContant
				, myPid
				, pMyself.GetAccount()
				, pMyself.GetHeroId()
				, pMyself.GetVip()
				, pMyself.GetName());

			_tmp_p->SaveChatRecord(type_chat_to_player, destId, pContant, pMyself);


			pMyself.PushChatMessageToClient(type, pContant
				, myPid
				, pMyself.GetAccount()
				, pMyself.GetHeroId()
				, pMyself.GetVip()
				, pMyself.GetName());

			destId = pMyself.GetPlayerId();
			pMyself.SaveChatRecord(type_chat_to_player, destId, pContant, pMyself);
			pMyself.SendChatRetMessageToClient(0);
		}
		else
		{
			//回复给自己：兄弟，对方没在线
			//ret:1
			pMyself.SendChatRetMessageToClient(1);
		}
		break;
	}
	default:
		break;
	}

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void CChatManager::SendChatMessageToGuild(uint32_t playerId, uint32_t type,
	uint32_t destId, const char* pContant, Player& pMy, uint32_t shareType)
{
	_safe_guard_begin;

	CGuild* guild = CGuildManager::GetInstance().GetGuildByID(destId);
	guild->SendChatMessageToGuild(playerId, type, destId, pContant, pMy, shareType);

	_safe_guard_end;
}

void CChatManager::SendChatMessageToGuild(uint32_t playerId, uint32_t type,
	uint32_t destId, const char* pContant, Player& pMy
	, const char* pGvcId, double dGvcTime, const char* pGvcPart)
{
	_safe_guard_begin;

	CGuild* guild = CGuildManager::GetInstance().GetGuildByID(destId);
	guild->SendChatMessageToGuild(playerId, type, destId, pContant, pMy
		, pGvcId, dGvcTime, pGvcPart);

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void CChatManager::SendChatToFriendMessageToClient(uint32_t myPid, uint32_t type,
	uint32_t destId, const char* pContant, uint32_t cType, Player& pMyself)
{
	_safe_guard_begin;

	Player* player = CSceneLogic::GetInstance().GetPlayerByPlayerID(destId);
	if (player != NULL)
	{
		player->PushChatMessageToFriendClient(type, pContant
			, myPid
			, pMyself.GetAccount()
			, pMyself.GetHeroId()
			, pMyself.GetVip()
			, pMyself.GetName()
			, destId
			, cType);

		pMyself.PushChatMessageToFriendClient(type, pContant
			, myPid
			, pMyself.GetAccount()
			, pMyself.GetHeroId()
			, pMyself.GetVip()
			, pMyself.GetName()
			, destId
			, cType);

		pMyself.SendFriendChatRetMessageToClient(destId, cType, 0);
	}
	else
	{
		pMyself.PushChatMessageToFriendClient(type, pContant
			, myPid
			, pMyself.GetAccount()
			, pMyself.GetHeroId()
			, pMyself.GetVip()
			, pMyself.GetName()
			, destId
			, cType);

		//记录离线聊天数据

		//回复给自己：对方没在线
		//ret:1

		pMyself.SendFriendChatRetMessageToClient(destId, cType, 1);
	}

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void CChatManager::SendChatToFriendMessageToClient(uint32_t myPid, uint32_t type,
	uint32_t destId, const char* pContant, uint32_t cType, Player& pMyself
	, const char* gvcId, double gvcTime, const char* gvcPart)
{
	_safe_guard_begin;

	Player* player = CSceneLogic::GetInstance().GetPlayerByPlayerID(destId);
	if (player != NULL)
	{
		player->PushChatMessageToFriendClient(type, pContant
			, myPid
			, pMyself.GetAccount()
			, pMyself.GetHeroId()
			, pMyself.GetVip()
			, pMyself.GetName()
			, destId
			, cType
			, gvcId
			, gvcTime
			, gvcPart);

		pMyself.PushChatMessageToFriendClient(type, pContant
			, myPid
			, pMyself.GetAccount()
			, pMyself.GetHeroId()
			, pMyself.GetVip()
			, pMyself.GetName()
			, destId
			, cType
			, gvcId
			, gvcTime
			, gvcPart);

		pMyself.SendFriendChatRetMessageToClient(destId, cType, 0);
	}
	else
	{
		pMyself.PushChatMessageToFriendClient(type, pContant
			, myPid
			, pMyself.GetAccount()
			, pMyself.GetHeroId()
			, pMyself.GetVip()
			, pMyself.GetName()
			, destId
			, cType
			, gvcId
			, gvcTime
			, gvcPart);

		// 记录离线聊天信息
		std::string chat_str;
		pMyself.getChatMessageJson(chat_str, type, pContant
			, myPid
			, pMyself.GetAccount()
			, pMyself.GetHeroId()
			, pMyself.GetVip()
			, pMyself.GetName()
			, destId
			, cType
			, gvcId
			, gvcTime
			, gvcPart);

		int32_t _ret = MapSlot::GetInstance().GetLuaManagerPtr()->RunScriptFunction(LUA_ChatFunctionScript,
			LUA_SavePlayerOfflineChat, (int32_t)myPid, chat_str.c_str());

		//回复给自己：对方没在线
		//ret:1
		pMyself.SendFriendChatRetMessageToClient(destId, cType, 1);
	}

	_safe_guard_end;
}

void CChatManager::DealChatEvent(uint32_t playerId, uint32_t chatEvent, const char* data)
{
	if (MapSlot::GetInstance().GetLuaManagerPtr()->TryLock())
	{
		int32_t _ret = MapSlot::GetInstance().GetLuaManagerPtr()->RunScriptFunction(LUA_ChatFunctionScript,
			LUA_ChatAchievement, (int32_t)playerId, (int32_t)chatEvent, data);
	}
	else
	{
		int32_t _ret = MapSlot::GetInstance().GetLuaManagerPtr()->RunScriptFunction(LUA_ChatFunctionScript,
			LUA_ChatAchievement, (int32_t)playerId, (int32_t)chatEvent, data);
	}
}